<?php

namespace App\Http\Controllers;

use App\Enum\CodeEnum;
use Illuminate\Http\Request;
use Laravel\Lumen\Routing\Controller as BaseController;

class Controller extends BaseController
{

    protected $request;
    protected $page = 1; //页数
    protected $size = 10; //默认每页条数
    public function __construct()
    {
        $this->request = new Request();
    }

    /**
     * send error json string
     * @param int $code
     * @param string $message
     * @return $this|\Illuminate\Http\JsonResponse
     */
    public function sendSuccess($message = '')
    {
        $method = $this->request->input('method');
        $callback = $this->request->input('callback');

        if ($method === 'jsonp' && $callback)
            return Response()->jsonp($callback, ['code' => CodeEnum::SUCCESS, 'msg' => $message ? $message : CodeEnum::message(CodeEnum::SUCCESS)]);

        $headers = ['content-type' => 'application/json'];
        return Response()->json(['code' => CodeEnum::SUCCESS, 'msg' => $message ? $message : CodeEnum::message(CodeEnum::SUCCESS)])
            ->withHeaders($headers);
    }

    /**
     * send error json string
     * @param string $message
     * @param int $code
     * @return $this|\Illuminate\Http\JsonResponse
     */
    public function sendError($message = '', $code = CodeEnum::ERROR)
    {
        $method = $this->request->input('method');
        $callback = $this->request->input('callback');

        if ($method === 'jsonp' && $callback)
            return Response()->jsonp($callback, ['code' => $code, 'msg' => $message ? $message : CodeEnum::message($code)]);

        $headers = ['content-type' => 'application/json'];
        return Response()->json(['code' => $code, 'msg' => $message ? $message : CodeEnum::message($code)])
            ->withHeaders($headers);
    }

    /**
     * send success json string
     * @param array $data
     * @return $this|\Illuminate\Http\JsonResponse
     */
    public function sendJson($data = [])
    {
        $method = $this->request->input('method');
        $callback = $this->request->input('callback');

        if ($method === 'jsonp' && $callback)
            return Response()->jsonp($callback, ['code' => CodeEnum::SUCCESS, 'data' => $data, 'msg' => CodeEnum::message(CodeEnum::SUCCESS)]);

        $headers = ['content-type' => 'application/json'];
        return Response()->json(['code' => CodeEnum::SUCCESS, 'data' => $data, 'msg' => CodeEnum::message(CodeEnum::SUCCESS)])
            ->withHeaders($headers);
    }
}
